/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package javacademic.autor;

import java.io.IOException;
import java.sql.Connection;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javacademic.Config;
import javax.servlet.RequestDispatcher;
/**
 *
 * @author leidson
 */
public class ListarAutorServlet extends HttpServlet {
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        ArrayList lautor = new ArrayList();
                
        try{
         String nome = request.getParameter("nome"); 
         if (nome == null || nome.trim().length() <= 0){
             nome = "%";
         } else{
             nome = "%"+nome+"%";
         }
             
         Class.forName("org.postgresql.Driver").newInstance();
         Connection connection = DriverManager.getConnection(Config.db_adress,Config.db_user,Config.db_password);
         Statement statement = connection.createStatement();
         String sql = "select codigo, nome, citacao from autor where upper(nome) like '"+nome.toUpperCase()+"' order by codigo";
         ResultSet resultSet = statement.executeQuery(sql);
         while (resultSet.next()){
             Autor a = new Autor();
             a.setNome(resultSet.getString("nome"));
             a.setCodigo(resultSet.getInt("codigo"));
             a.setCitacao(resultSet.getString("citacao"));
             lautor.add(a);
         }
         statement.close();
         connection.close();
        }
        catch (SQLException ex) {
            Logger.getLogger(ListarAutorServlet.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(ListarAutorServlet.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(ListarAutorServlet.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(ListarAutorServlet.class.getName()).log(Level.SEVERE, null, ex);
        }
        request.setAttribute("lautor", lautor);
        
        RequestDispatcher requestDispatcher = request.getRequestDispatcher("WEB-INF/jsp/autor/listar.jsp");
        requestDispatcher.forward(request, response);
        
    } 

    // <editor-fold defaultstate="collapsed" desc="Métodos HttpServlet. Clique no sinal de + à esquerda para editar o código.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
